package com.fz.controller;

import com.fz.entity.Book;
import com.fz.mapper.BookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by webrx on 2017/7/7.
 */
@Controller // @RequestMapping("/book")
public class BookIndex {

    @Autowired
    private BookMapper books;

    @RequestMapping("/index01")
    public ModelAndView index(Integer id){
        ModelAndView mv = new ModelAndView("index");
        //List<Book> list =books.queryAll();
        int aa=1;
        int bb=2;
        try{
            if(id > 0) {
                aa=id;
                bb=2;
            }else {
                aa=1;
                bb=2;
            }
        }catch (Exception e){
           e.printStackTrace();
        }


        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("pagesize",bb);
        map.put("pagecount",aa*bb-bb);
        List<Book> list = books.page(map);
        mv.addObject("book", list);
        mv.addObject("num",aa);
        return mv;
    }

    @RequestMapping("/delete")
    public String delete(int id ){
        books.deleteById(id);
        System.out.println(id);
        return "redirect:/index01";
    }
    @RequestMapping("/update")
     public ModelAndView update(int id){
        ModelAndView mv =new ModelAndView("upda");
        Book book =books.queryById(id);
        mv.addObject("book",book);
        return mv;
     }
    @RequestMapping("/update02")
    public String update02(Book book){
        books.updateById(book);
        return "redirect:index01" ;
    }



    @RequestMapping("/insert")
    public String insert(){
        return "add";
    }

    @RequestMapping("/insert02")
    public String insert02(Book book){
        books.insert(book);
        return "redirect:index01";
    }




}
